package com.codedancer.campus.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codedancer.campus.common.core.BaseMapper;
import com.codedancer.campus.po.SystemMessage;
import com.codedancer.campus.qo.systemMessage.SystemMessageSearchQo;

public interface SystemMessageMapper extends BaseMapper<SystemMessage> {

    /**
     * 分页查询系统消息(客户端)
     *
     * @param systemMessagePage
     * @param systemMessageSearchQo
     * @return
     */
    default IPage<SystemMessage> pageToClient(Page<SystemMessage> systemMessagePage,
        SystemMessageSearchQo systemMessageSearchQo) {
        LambdaQueryWrapper<SystemMessage> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
            .eq(SystemMessage::getReceiveUserUuid, systemMessageSearchQo.getCurrentUserUuid());
        lambdaQueryWrapper.orderByDesc(SystemMessage::getCreateTime);
        return selectPage(systemMessagePage, lambdaQueryWrapper);
    }

    /**
     * 消息全部已读
     *
     * @param currentUserUuid
     */
    default void allRead(String currentUserUuid) {
        LambdaUpdateWrapper<SystemMessage> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(SystemMessage::getReceiveUserUuid, currentUserUuid);
        lambdaUpdateWrapper.set(SystemMessage::getIsRead, true);
        update(null, lambdaUpdateWrapper);
    }

    /**
     * 是否存在未读系统消息
     *
     * @param currentLoginUuid
     * @return
     */
    default Boolean isExistsNoRead(String currentLoginUuid) {
        LambdaQueryWrapper<SystemMessage> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SystemMessage::getReceiveUserUuid, currentLoginUuid);
        lambdaQueryWrapper.eq(SystemMessage::getIsRead, false);
        return selectCount(lambdaQueryWrapper) > 0;
    }
}
